package com.xtaller.easy.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.easy.admin.base.TServiceImpl;
import com.xtaller.easy.admin.mapper.SysLogMapper;
import com.xtaller.easy.admin.model.SysLog;
import com.xtaller.easy.admin.service.ISysLogService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import com.xtaller.common.convert.W;
import com.xtaller.common.bean.Where;
/**
* Created by Taller on 2018/05/11
*/
@Service
public class SysLogService extends TServiceImpl<SysLogMapper,SysLog> implements ISysLogService {
   /**************************CURD begin******************************/ 
   // 创建
   @Override
   public SysLog createModel(SysLog model) {
       if(this.insert(model))
           return model;
       return null;
   }
 
   // 删除
   @Override
   public Boolean deleteModel(Object ids,String reviser) {
       return this.delete(ids,reviser);
   }
 
   // 修改
   @Override
   public SysLog updateModel(SysLog model) {
       if(this.update(model))
           return model;
       return null;
   }
 
   // 查询
   @Override
   public List<SysLog> findByIds(Object ids) {
       return this.selectByIds(ids);
   }
 
   // 属于
   @Override
   public Boolean exist(List<Where> w) {
       w.add(new Where("1"));
       return this.query(w).size() > 0;
   }
 
   // 查询一个id是否存在
   @Override
   public Boolean existId(Object id) {
       where = W.f(
               W.and("id","eq",id),
               W.field("1")
       );
       return this.query(where).size() > 0;
   }
 
   // 获取所有
   @Override
   public List<SysLog> getData() {
       where = W.f(
               W.and(1,"eq",1)
       );
       return this.query(where);
   }



    /**************************CURD end********************************/
    @Override
    public JSONObject getPageList(int index, int pageSize, String where) {
        JSONObject result = new JSONObject();

        result.put("rows", new ArrayList<>());
        // 总记录数
        JSONObject row = baseMapper.getPageCount(where);
        int totalCount = row.getInteger("total");
        result.put("total", totalCount);
        if(totalCount == 0)
            return result;
        // 分页数据
        index = index < 0 ? 1:index;
        int limit = (index - 1) * pageSize;
        result.put("rows", baseMapper.getPage(where, limit, pageSize));

        return result;
    }

    @Override
    public List<JSONObject> getApiLog(int day) {
        if(day >= 0)
            return baseMapper.getApiLog(day);
        return baseMapper.getApiLogs();
    }

    @Override
    public JSONObject getCount() {
        return baseMapper.getCount();
    }
}